package com.example.workbench.controller;

import com.example.settings.domain.User;
import com.example.utils.DateTimeUtil;
import com.example.utils.PrintJson;
import com.example.utils.ServiceFactory;
import com.example.utils.UUIDUtil;
import com.example.workbench.domain.ActivityRemark;
import com.example.workbench.service.ActivityRemarkService;
import com.example.workbench.service.impl.ActivityRemarkServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityRemarkController extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入到市场活动备注控制器");
        String path = request.getServletPath();
        if("/workbench/activityRemark/showRemarkList.do".equals(path)){
            getActivityRemarkByAid(request,response);
        }else if ("/workbench/activityRemark/deleteRemark.do".equals(path)){
            deleteRemark(request,response);
        }else if ("/workbench/activityRemark/saveRemark.do".equals(path)){
            saveRemark(request,response);
        }else if ("/workbench/activityRemark/updateRemark.do".equals(path)){
            updateRemark(request,response);
        }
    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String noteContent = request.getParameter("noteContent");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User)request.getSession().getAttribute("user")).getName();
        String editFlag = "1";

        ActivityRemark ar = new ActivityRemark();

        ar.setId(id);
        ar.setNoteContent(noteContent);
        ar.setEditFlag(editFlag);
        ar.setEditBy(editBy);
        ar.setEditTime(editTime);

        ActivityRemarkService as = (ActivityRemarkService) ServiceFactory.getService(new ActivityRemarkServiceImpl());

        boolean flag = as.updateRemark(ar);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("success", flag);
        map.put("ar", ar);

        PrintJson.printJsonObj(response, map);
    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {
        String noteContent = request.getParameter("noteContent");
        String activityId = request.getParameter("activityId");
        String id = UUIDUtil.getUUID();
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        ActivityRemark ar = new ActivityRemark();
        ar.setId(id);
        ar.setNoteContent(noteContent);
        ar.setActivityId(activityId);
        ar.setCreateBy(createBy);
        ar.setCreateTime(createTime);
        ar.setEditFlag(editFlag);

        ActivityRemarkService as = (ActivityRemarkService) ServiceFactory.getService(new ActivityRemarkServiceImpl());

        boolean flag = as.saveRemark(ar);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("success", flag);
        map.put("ar", ar);

        PrintJson.printJsonObj(response, map);
    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ActivityRemarkService asr = (ActivityRemarkService) ServiceFactory.getService(new ActivityRemarkServiceImpl());
        boolean flag = asr.deleteRemark(id);
        PrintJson.printJsonFlag(response,flag);
    }

    private void getActivityRemarkByAid(HttpServletRequest request, HttpServletResponse response) {

        String aid = request.getParameter("activityId");
        ActivityRemarkService ars = (ActivityRemarkService) ServiceFactory.getService(new ActivityRemarkServiceImpl());
        List<ActivityRemark> ar= ars.getActivityRemarkByAid(aid);
        PrintJson.printJsonObj(response,ar);
    }
}
